Skip to content

Snapshot LSP #1505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 114 commits into from
Aug 21, 2025
Merged

Snapshot LSP #1505

merged 114 commits into from
Aug 21, 2025

Conversation

andrewbranch
Copy link
Member

@andrewbranch andrewbranch commented Aug 2, 2025

Note

Diagrams are generated by Copilot with Python; they’re pretty rough but get the idea across. I used them for an internal team presentation and figured they’re still better than nothing for this.

Summary

This PR replaces the project system backing the LSP server with an immutable snapshot-based architecture.

Problem Statement

The original LSP implementation here in typescript-go inherited microsoft/TypeScript's architecture, which relied on mutable data structures suitable for synchronous operation. However, as we started to parallelize request handling, this approach became unsustainable. We were starting to spend a lot of time chasing down data races, for which the solution was often tacking on yet another mutex for an individual component.

Design Goals

The new architecture addresses these issues through immutable snapshots that can be safely read by multiple goroutines and efficiently cloned to create new state. This enables:

  • Elimination of data races with less complex mutex coordination
  • Support for multiple concurrent clients accessing the same workspace state (e.g. LSP, API, MCP)
  • Simplified reasoning about state changes and their effects

Architecture

Core Concepts

The system centers around snapshots - immutable representations of LSP server state at a point in time. Snapshots can be read concurrently without coordination and cloned to produce new snapshots when state changes are needed.

While TypeScript programs were already immutable, additional components needed to be refactored for immutability:

  • File state: Text content, line maps, and open/closed state from client
  • Project state: Loaded projects, file associations, and program freshness tracking
  • Config state: tsconfig existence and freshness of parsed options and files/include/exclude evaluation

System Structure

LSP Server Hierarchy

The architecture separates concerns between mutable session management and immutable snapshot state:

Session Layer (Mutable)

  • File change and request handlers called by LSP server
  • Live filesystem view and overlay state management
  • Current snapshot reference and lifecycle management
  • Ref-counting AST caches
  • Side effect triggering after snapshot transition

Snapshot Layer (Immutable)

  • Cached filesystem state at snapshot creation time
  • Open file state snapshot
  • Complete project collection with programs and configurations

State Transitions

Snapshot Creation Process

State changes occur through snapshot cloning:

  1. Session accumulates pending changes from LSP events and file watching
  2. Changes combine with current open file state to create new overlay state and summary of changes
  3. Builders handle changes with copy-on-write strategy against previous snapshot state, and finalize into new snapshot
  4. Session triggers logging, file watcher updates, ATA updates, and diagnostics refresh requests by comparing previous snapshot with newly adopted snapshot.

Request Processing

LSP Request Flow

When an LSP request needs to invoke a language service operation for a document, we first check if there are any pending changes that need to be applied (e.g. file watchers have been triggered, open files have been changed, or an ATA request has completed). If so, the Session's current snapshot is cloned to incorporate the pending changes while simultaneously ensuring that the default project for the requested file is loaded and up-to-date. If there are no pending changes, we check if the Session's current snapshot is capable of serving that request (that is, if a default project can be found for that file and the project is up-to-date). If not, we clone the snapshot, in this case with no file changes, but a request to load or update the default project for the requested file. Finally, we return a language service that uses the project from the latest snapshot.

Notable behavior changes

  • When searching project references to find a default project for a file, the previous default project finder would race on returning a referenced project that contains the target file, but it would also continue loading every referenced project recursively, and keep these projects open until the next file open request. The new system guarantees that when multiple references contain the target file, the one returned is the one at the lowest index in the searched tsconfig.json's references array, and it terminates work as soon as higher priority matches have been ruled out. In order to support this early termination of work, we can no longer keep every project we loaded open, since that set is nondeterministic. Instead, we keep open only the projects that led us to the one that ultimately contained the target file. Example:
    • Target file: /workspace/packages/foo/test.ts
    • Load /workspace/packages/foo/tsconfig.json, doesn't contain the file, has two references:
      • /workspace/packages/bar/tsconfig.json
      • /workspace/packages/baz/tsconfig.json
    • Load the two references in parallel; neither contains the file
    • Load /workspace/tsconfig.json, doesn't contain the file, has 1,000 references
    • Load all 1,000 references in a parallel work group; none contains the file, but collects 100 additional referenced projects. One of the 1,000 projects called tests.tsconfig.json had a reference of foo.test.tsconfig.json
    • Kick off loading for all 100 references in a parallel work group; foo.test.tsconfig.json contains the file at index 50
    • Wait for these 100 jobs to be done; ones with index > 50 are skipped.
    • No other project contains the file, so foo.test.tsconfig.json is the default project.
    • Projects we keep open after this process:
      • /workspace/foo.test.tsconfig.json (the default project)
      • /workspace/tests.tsconfig.json
      • /workspace/tsconfig.json
      • /workspace/packages/foo/tsconfig.json
    • The last 3 will be closed after the next file open, assuming they don't contribute to finding a default project for another file in the same way again.
    • Projects we used to keep open after this process: all 1,100+ we loaded
  • There is only one inferred project. Strada had the capability to have multiple inferred projects for different workspace roots, and some of this infrastructure was ported into Corsa, but it was only used in tests, because LSP differs significantly from TS Server in how files get assigned to inferred projects. If multiple inferred projects are desired in Corsa, the feature needs to be redesigned from the ground up.

Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anything left? I've been playing with this and can't break it, even with the race detector enabled.

@andrewbranch
Copy link
Member Author

I realized while gone that I forgot the file watchers for ATA locations. Working on that now.

}

func NewConfiguredProject(
configFileName string,
configFilePath tspath.Path,
host ProjectHost,
builder *projectCollectionBuilder,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NewConfiguredProject is exported, but it takes a *projectCollectionBuilder, which isn’t exported. That means code outside the package can’t actually call this function. Was the intent for the constructor to be internal (unexported), or for projectCollectionBuilder to be exported?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions aren't referenced outside the package, I think, so it's somewhat innocuous given the internalness of the whole API (we have worse examples elsewhere... but usually not in this part of the code). Are you expecting to need to call any of these in tsgolint and that's the interest in having these available?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two reasons really:

  • from tsgolint, we aren't expecting to be able to call these, but it breaks how we generate our shims (we can always work around this so feel free to tell me to do that 🙂 ), as the inconsistency means that we're trying to reference private types.
  • from a code pov, it doesn't really make sense for one to be public, and not, as it means NewConfiguredProject can't be called outside the package, despite it being exported?

That said, feel free to ignore these cmts and i can workaround in the tsgolint side.

// NewSnapshot
func NewSnapshot(
id uint64,
fs *snapshotFS,
Copy link
Contributor

@camc314 camc314 Aug 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment here, NewSnapshot is public, but snapshotFS is not?

Same comment with extendedConfigCache on L45

Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏

@andrewbranch andrewbranch added this pull request to the merge queue Aug 21, 2025
Merged via the queue into microsoft:main with commit a6bad90 Aug 21, 2025
22 checks passed
@andrewbranch andrewbranch deleted the snapshots branch August 21, 2025 18:58
@andrewbranch
Copy link
Member Author

@camc314 I'm merging now but happy to follow up with changes. To answer your questions broadly, the change most aligned with my intentions would be to make NewSnapshot and NewProject unexported; the only way you're supposed to get a reference to them is through Session. I would eventually like a way for API consumers to speculate on a snapshot change; i.e. call snapshot.Clone directly, or have a method through Session to help with that, but that wasn't in my first string of priorities. There is very little in here that is tailored to external API usage, but over time I anticipate needing to add more functionality to integrate with the IPC API to support LS plugins, maybe MCP usage, etc., and I think those kinds of changes will be similar to what tsgolint would like to see.

graphite-app bot pushed a commit to oxc-project/tsgolint that referenced this pull request Aug 22, 2025
…d types (#128)

microsoft/typescript-go#1505 makes some changes that mean that some functions reference types that aren’t exported, this means that we can’t compile tsgolint anymore 😥

this PR skips generating shims for functions with unexported types, logging a warning to stderr.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants